package com.yanqu.road.dao.impl.activity.xiaowuRecall;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.xiaowuRecall.XiaowuRecallComb;
import com.yanqu.road.entity.activity.xiaowuRecall.XiaowuRecallText;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class XiaowuRecallConfigDaoImpl extends TempDao {
    private static String getXiaowuRecallCombSql = "select * from `t_s_activity_douluo_smalldance` where `activityId`=?";

    public List<XiaowuRecallComb> getXiaowuRecallCombList(int activityId) {
        List<XiaowuRecallComb> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getXiaowuRecallCombSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                XiaowuRecallComb xiaowuRecallComb = new XiaowuRecallComb();
                xiaowuRecallComb.setCellId(rs.getInt("cellId"));
                xiaowuRecallComb.setType(rs.getInt("type"));
                xiaowuRecallComb.setRewards(rs.getString("rewards"));
                xiaowuRecallComb.setComb(StringUtils.stringToIntegerList(rs.getString("combination"), ";"));
                list.add(xiaowuRecallComb);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getXiaowuRecallTextSql = "select * from `t_s_activity_smalldance_text` where `activityId`=?";
    public List<XiaowuRecallText>getXiaowuRecallTextList(int activityId){
        List<XiaowuRecallText> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getXiaowuRecallTextSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                XiaowuRecallText xiaowuRecallText = new XiaowuRecallText();
                xiaowuRecallText.setId(rs.getInt("id"));
                xiaowuRecallText.setPageNum(rs.getInt("page"));
                xiaowuRecallText.setValue(rs.getString("value"));
                list.add(xiaowuRecallText);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
